// index.js
Page({
  data: {
    elementValue: null,
    productId: ''
  },
  fetchData: function() {
    wx.request({
      url: 'http://localhost:8080/purchase/selectAll',
      success: (res) => {
        const data = res.data.data;
        this.setData({
          elementValue: data
        });
      }
    });
  },
  seckill: function(event) {
    const id = wx.getStorageSync('userId');
    const productId = event.currentTarget.dataset.id;
    console.log(id + "判断购买的用户是否存在");
    console.log(productId + "商品id");
    if (id == null || id == 0) {
      wx.showToast({
        title: '请登录进行购买',
        icon: 'none'
      });
    } else {
      wx.request({
        url: 'http://localhost:8080/order/insert',
        method: 'POST',
        data: {
          userId: id,
          productId: productId,
          status: 0
        },
        success: (res) => {
          const data = res.data.code;
          if (data == 200) {
            wx.showToast({
              title: '购买成功',
            });
          }
        },
        fail: (res) => {
          wx.showToast({
            title: '购买失败，请重试',
            icon: 'none'
          });
        }
      });
    }
  },
  onLoad: function() {
    this.fetchData();
  }
});